package com.jiangyifen.robot;

import java.util.concurrent.CountDownLatch;

public class CountDown {

	public static void main(String[] args) throws InterruptedException {
		CountDownLatch startSignal = new CountDownLatch(1);
		CountDownLatch doneSignal = new CountDownLatch(3);

		for (int i = 0; i < 3; ++i) {// create and start threads
			new Thread(new Worker1(startSignal, doneSignal)).start();
		}

		System.out.println("111"); // don't let run yet
		startSignal.countDown(); // let all threads proceed
		System.out.println("222");
		doneSignal.await(); // wait for all to finish
	}

}

class Worker1 implements Runnable {
	private final CountDownLatch startSignal;
	private final CountDownLatch doneSignal;

	Worker1(CountDownLatch startSignal, CountDownLatch doneSignal) {
		this.startSignal = startSignal;
		this.doneSignal = doneSignal;
	}

	public void run() {
		try {
			startSignal.await();
			System.out.println("do work");
			doneSignal.countDown();
		} catch (InterruptedException ex) {
		} // return;
	}

}